package com.kucun.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import sun.print.resources.serviceui;

import com.kucun.bean.Clientinfo;
import com.kucun.bean.Goods;
import com.kucun.bean.Page;
import com.kucun.bean.Users;
import com.kucun.service.GoodsService;
import com.kucun.util.UID;
import com.times.util.DateUtils;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
@RequestMapping("/goods")
public class GoodsController {

	@Autowired
	private GoodsService Goodservice;
	
	/**
	 * 
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getallgoods.html")
	public String getAllUser(int rows,int page,HttpServletRequest request,HttpServletResponse response) throws IOException{
       
        String q=request.getParameter("q");
        String field=request.getParameter("field");
        
        String searchertext=request.getParameter("searchertext");
        if(q!=null){
			field="specifications";
		}
        if("".equals(searchertext)||searchertext==null){
        	searchertext=q;
        }else{
        	searchertext = URLDecoder.decode(searchertext, "utf-8");
        } 
		Page page1=new Page();
		page1.setI(rows*(page-1));
		page1.setRows(rows);
		page1.setFiled(field);
		page1.setSearchertext(searchertext);
		try {
			String a=request.getParameter("a");
			page1.setObj(a);
		} catch (Exception e) {
			// TODO: handle exception
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		List<Goods> list = Goodservice.findAll(page1);	
		jsonMap.put("total",Goodservice.getCount(page1).getTotal());//total键 存放总记录数，必须的  
        jsonMap.put("rows",list);//rows键 存放每页记录 list  
        JSONObject jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	@RequestMapping("/getallgoods1.html")
	public String getAllUser1(HttpServletRequest request,HttpServletResponse response) throws IOException{
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		List<Goods> list = Goodservice.findAll();	
		jsonMap.put("rows",list);//rows键 存放每页记录 list  
        JSONObject jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	@RequestMapping("/getallgood.html")
	public String getAll(int rows,int page,HttpServletRequest request,HttpServletResponse response) throws IOException{
        Page page1=new Page();
		page1.setI(rows*(page-1));
		page1.setRows(rows);
		List<Goods> list = Goodservice.findAll(page1);	
		JSONArray jsonObject = JSONArray.fromObject(list);
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	
	@RequestMapping("/getallgoodstoecharts.html")
	public String getAll1(HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Goods> list = Goodservice.findAll();	
		JSONArray jsonObject = JSONArray.fromObject(list);
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	/**
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddUser.html")
	public String toAddUser(HttpServletRequest request){
		
		return "/addUser";
	}
	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/addgoods.html")
	public String addgoods(Goods goods,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String tag=request.getParameter("tag");
		Users user=(Users)request.getSession().getAttribute("user");
		int s=0;
		PrintWriter pw = response.getWriter();
		if("1".equals(tag)){
			int ss=Goodservice.updateByPrimaryKeySelective(goods);
			if(ss>0){
				 pw.write("ok");
			}else{
				 pw.write("err");
			}
		}else{
			String billon="WP"+UID.next();
			goods.setId(null);
			goods.setBilldate(new Date());
			goods.setCreatebyid(user.getUserid());
			goods.setCreatebyname(user.getUsername());
			goods.setBillno(billon);
			s=Goodservice.save(goods);
			if(s>0){
				 pw.write("ok");
			}else{
				 pw.write("err");
			}
		}
		return null;
	}
	
	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/updategoods.html")
	public String updateUser(String goodsname,Goods user,HttpServletRequest request,HttpServletResponse response) throws IOException{
		int s=Goodservice.updateGoodsForDel(goodsname);
		PrintWriter out = response.getWriter();
		if(s>0){
			 out.write("ok");
		}else{
			 out.write("err");
		}
		return null;
	}
	/**
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getUser.html")
	public String getUser(int id,HttpServletRequest request){
		
		request.setAttribute("user", Goodservice.findById(id));
		return "/editUser";
	}
	/**
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delgoods.html")
	public void delUser(String ids,HttpServletRequest request,HttpServletResponse response){
		System.out.println(ids);
		String result = "{\"result\":\"error\"}";
		
		if(Goodservice.delete(ids)){
			result = "{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			PrintWriter out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 允许出库的物品
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getgoodsexistout.html")
	public String GetGoodsExistOut(int rows,int page,HttpServletRequest request,HttpServletResponse response) throws IOException{
        String clientid=request.getParameter("clientid");
        System.out.println("clientid======"+clientid);
		Page page1=new Page();
		page1.setI(rows*(page-1));
		page1.setRows(rows);

		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		List<Goods> list = Goodservice.GetGoodsExistOut(page1);	
		jsonMap.put("total",Goodservice.GetGoodsExistOutCount(page1).getTotal());//total键 存放总记录数，必须的  
        jsonMap.put("rows",list);//rows键 存放每页记录 list  
        JSONObject jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	
}
